﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;

namespace DianHanDemo01
{
    public class MainViewModel : INotifyPropertyChanged
    {
        private ObservableCollection<MyUserControlItem> _items;

        public MainViewModel()
        {
            _items = new ObservableCollection<MyUserControlItem>();

            // 初始化第一批控件
            InitializeItems(10, 49);
        }

        public ObservableCollection<MyUserControlItem> Items
        {
            get => _items;
            set
            {
                _items = value;
                OnPropertyChanged();
            }
        }

        public async void StartUpdating()
        {
            while (true)
            {
                await Task.Delay(5000); // 每隔5秒更新一次


                InitializeItems(10, 49);
            }
        }

        private void InitializeItems(int rows, int columns)
        {
            Items.Clear();
            for (int row = 0; row < rows; row++)
            {
                for (int col = 0; col < columns; col++)
                {
                    Items.Add(new MyUserControlItem
                    {
                        Row = row,
                        Column = col,
                        RowNumber = row + 1,
                        IsFirstInRow = col == 0
                    });
                }
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    public class MyUserControlItem
    {
        public int Row { get; set; }
        public int Column { get; set; }
        public int RowNumber { get; set; }
        public bool IsFirstInRow { get; set; }
    }
}
